<?php

use Phalcon\DI\FactoryDefault\CLI as CliDI,
	Phalcon\CLI\Console as ConsoleApp;


// Define global constants
define( 'DS'	, DIRECTORY_SEPARATOR );
define( 'BASE' 	, __DIR__);

// Init new Cli factory
$di = new CliDI();

//Register an autoloader
$loader = new \Phalcon\Loader();

$loader->registerDirs(
	array(
		BASE . DS . '..' . DS . 'tasks',
		BASE . DS . '..' . DS . 'models'
	)
)->register();


// Setting Database
$dbConfig = new Phalcon\Config\Adapter\Ini(__DIR__ . DS . '..' . DS . 'config' . DS . 'database.ini');

$di->set('db', function() use ($dbConfig) {
	return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
		"host" => $dbConfig->database->host,
		"username" => $dbConfig->database->username,
		"password" => $dbConfig->database->password,
		"dbname" => $dbConfig->database->name
	));
});

//Create a console application
$console = new ConsoleApp();
$console->setDI($di);

try {
	// handle incoming arguments
	$console->handle();
}
catch (\Phalcon\Exception $e) {
	echo $e->getMessage();
	exit(255);
} catch (PDOException $e){
	echo "PDOException: ", $e->getMessage();
	exit(255);
}